import { autoLogin } from '@/apis/userAPI'
import { LoginModel } from '@/types/model'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export default defineStore(
  'token',
  () => {
    const token = ref('')
    const login = async (value: LoginModel) => {
      const {
        data: { token: t },
      } = await autoLogin(value)
      token.value = t
    }

    const exitLogin = () => {
      localStorage.removeItem('token')
    }
    return { token, login, exitLogin }
  },
  { persist: true },
)
